package com.rslai.commons.validator.jsonvalidator.util;

/**
 * 字符串解码工具类
 */
public final class StringUnmarshaller {

    /**
     * 解码
     * @param str
     * @return
     */
	public static String unmarshall(String str) {
		str = str.substring(1, str.length() - 1);

		int len = str.length();
		StringBuilder sb = new StringBuilder(len);
		int i = 0;
		while (i < len) {
			char c = str.charAt(i);
			++i;
			if (c == '\\') {
				char c1 = str.charAt(i);
				++i;
				if (c1 == 'u') { // unicode字符
                    // 处理4个字节的unicode字符
					int codePoint = getCodePoint(str, i);
					sb.appendCodePoint(codePoint);
					i += 4;
				} else {
					char escapedChar = getEscapedChar(str, c1);
					sb.append(escapedChar);
				}
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

    /**
     * 返回unicode字符
     * @param str
     * @param i
     * @return
     */
	private static int getCodePoint(String str, int i) {
		String s = str.substring(i, i + 4);
		int codePoint = Integer.parseInt(s, 16);
		return codePoint;
	}

    /**
     * 返回控制字符
     * @param str
     * @param c
     * @return
     */
	private static char getEscapedChar(String str, char c) {
		char ch;
		switch (c) {
		case 'n':
			ch = '\n';
			break;
		case 'b':
			ch = '\b';
			break;
		case 'f':
			ch = '\f';
			break;
		case 't':
			ch = '\t';
			break;
		case 'r':
			ch = '\r';
			break;
		case '\"':
			ch = '\"';
			break;
		case '\'':
			ch = '\'';
			break;
		case '\\':
			ch = '\\';
			break;
		case '/':
			ch = '/';
			break;
		default:
			throw new IllegalStateException("Unexpected character: " + c + " in " + str);
		}
		return ch;
	}
}